//
//  MainTabBarController.m
//  MinesTime
//
//  Created by imac on 2017/10/24.
//  Copyright © 2017年 MS1968. All rights reserved.
//

#import "MainTabBarController.h"
#import "MeVC.h"
#import "MainVC.h"
#import "UgcMain_VC_WX.h"
#import "Ugc_Release_VC_WX.h"
#import "MainFirst_WX.h"
#import "UGC_Release_View_WX.h"
#import "InquiryMain_VC_WX.h"
#import "Tipe_WX_VC.h"
#import "Auction_Main_VC_WX.h"


@interface MainTabBarController ()<UITabBarDelegate,UITabBarControllerDelegate>

@property (nonatomic,assign)NSInteger itemTitle;

@end

@implementation MainTabBarController

/* 拦截发布页面点击 */
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(nonnull UIViewController *)viewController{
    /**
     2019.03。13
    UINavigationController *navCtrl = (UINavigationController *)viewController;

    UIViewController *rootCtrl = navCtrl.topViewController;

    if([rootCtrl isKindOfClass:[Ugc_Release_VC_WX class]]) {
  
        return NO;
    }
     */
    return YES;
}

/* 展示发布页面 */
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    
    /**
     2019.03。13
     if (item.tag == 3) {
     
     UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
     
     UIViewController * = [self getCurrentVCFrom:rootViewController];currentVC
     [DEFAULReleaseV cleanData];
     DEFAULReleaseV.frame = CGRectMake(0, kScreenH, kScreenW, kScreenH);
     
     if (kUSER_INFO_MANAGER.userIsLogin) {
     
     [UIView animateWithDuration:0.3 animations:^{
     DEFAULReleaseV.frame = CGRectMake(0, -20, kScreenW, kScreenH);
     } completion:^(BOOL finished) {
     [UIView animateWithDuration:0.2 animations:^{
     DEFAULReleaseV.frame = CGRectMake(0, 0, kScreenW, kScreenH);
     }];
     }];
     [[UIApplication sharedApplication].keyWindow addSubview:DEFAULReleaseV];
     
     
     [currentVC.view addSubview:DEFAULReleaseV];
     [currentVC.tabBarController.tabBar setHidden:YES];
     DEFAULReleaseV.vc = currentVC;
     
     }else{
     [Tools pushSinge];
     }
     }
     */
   
    
   
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    self.delegate = self;
  
}

-(void)def:(NSInteger)index{
    
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        [self setupChildVC2];
    }else{
        [self setupChildVC];
    }
    
    self.tabBar.tintColor = [UIColor colorWithHex:@"#363A4F"]; //文字
    self.tabBar.barTintColor = [UIColor whiteColor]; //背景
    
    [self.tabBar setShadowImage:[self imageWithColor:[[UIColor colorWithHex:@"#C5C5C5"] colorWithAlphaComponent:0.5] size:CGSizeMake(1, 1)]];
    [self.tabBar setBackgroundImage:[[UIImage alloc] init]];
    self.tabBarController.tabBar.delegate = self;
    
    [self setTabbarBackColor];
    
}

//解决我的页面侧滑tabbar显示下个vc颜色
-(void)setTabbarBackColor{
    CGRect fram = self.tabBar.bounds;
    
    fram.size.height = 200;
    
    // 设置一个自定义 View,大小等于 tabBar 的大小
    UIView *bgView = [[UIView alloc] initWithFrame:fram];
    // 给自定义 View 设置颜色
    bgView.backgroundColor = [UIColor whiteColor];
    // 将自定义 View 添加到 tabBar 上
    [self.tabBar insertSubview:bgView atIndex:0];
}


-(UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;

}

- (void)setupChildVC2 {
    
    NSArray *vcParamsArr = @[@{@"Controller":[[Tipe_WX_VC alloc]init],
                               @"itemTitle":@"快捷",
                               @"itemImage":@"tab_ban_Inquiry",
                               @"itemImageSelected":@"tab_ban_Inquiry_s",
                               @"tag":@(1)},
                             @{@"Controller":[[MainFirst_WX alloc]init],
                               @"itemTitle":@"精选",
                               @"itemImage":@"tab_btn_home",
                               @"itemImageSelected":@"tab_btn_home_s",
                               @"tag":@(2)},
                             @{@"Controller":[[UgcMain_VC_WX alloc]init],
                               @"itemTitle":@"动态",
                               @"itemImage":@"tab_btn_mine",
                               @"itemImageSelected":@"tab_btn_mine_s",
                               @"tag":@(3)},
                             @{@"Controller":[[InquiryMain_VC_WX alloc]init],
                               @"itemTitle":@"寻表",
                               @"itemImage":@"tab_ban_Inquiry",
                               @"itemImageSelected":@"tab_ban_Inquiry_s",
                               @"tag":@(4)},
                             @{@"Controller":[[MeVC alloc]init],
                               @"itemTitle":@"我的",
                               @"itemImage":@"tab_btn_mine",
                               @"itemImageSelected":@"tab_btn_mine_s",
                               @"tag":@(5)}];
    
    
    NSMutableArray<UIViewController *> *vcArrM = [NSMutableArray array];
    for (NSDictionary *params in vcParamsArr) {
        UIViewController *vc = [self createChildVCWithDict:params];
        if (![vcArrM containsObject:vc]) {
            [vcArrM addObject:vc];
        }
    }
    //    [self setItemImageFrame:vcArrM];
    self.viewControllers = vcArrM.copy;
}

- (void)setupChildVC {
    // LocalizedStringByKey(@"stringKey_0001")

    NSArray *vcParamsArr = @[@{@"Controller":[[MainFirst_WX alloc]init],
                               @"itemTitle":@"精选",
                               @"itemImage":@"tab_btn_home",
                               @"itemImageSelected":@"tab_btn_home_s",
                               @"tag":@(1)},
                             @{@"Controller":[[UgcMain_VC_WX alloc]init],
                               @"itemTitle":@"动态",
                               @"itemImage":@"tab_btn_social",
                               @"itemImageSelected":@"tab_btn_social_s",
                               @"tag":@(2)},
                             @{@"Controller":[[Auction_Main_VC_WX alloc]init],
                               @"itemTitle":@"交易",
                               @"itemImage":@"tab_ban_Inquiry",
                               @"itemImageSelected":@"tab_ban_Inquiry_s",
                               @"tag":@(3)},
                             @{@"Controller":[[MainVC alloc]init],
                               @"itemTitle":@"寻表",
                               @"itemImage":@"tab_btn_search",
                               @"itemImageSelected":@"tab_btn_search_s",
                               @"tag":@(4)},
                             @{@"Controller":[[MeVC alloc]init],
                               @"itemTitle":@"我的",
                               @"itemImage":@"tab_btn_mine",
                               @"itemImageSelected":@"tab_btn_mine_s",
                               @"tag":@(5)}];
    
    NSMutableArray<UIViewController *> *vcArrM = [NSMutableArray array];
    for (NSDictionary *params in vcParamsArr) {
        UIViewController *vc = [self createChildVCWithDict:params];
        if (![vcArrM containsObject:vc]) {
            [vcArrM addObject:vc];
        }
    }
//    [self setItemImageFrame:vcArrM];
    self.viewControllers = vcArrM.copy;
}


-(void)setItemImageFrame:(NSArray*)arr{
    
    //偏移按钮
    UIViewController *vc = arr[2];
    vc.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0,-6,0);
}

- (UIViewController *)createChildVCWithDict:(NSDictionary *)dict {
    
    // 控制器属性
    BaseVC *Controller = dict[@"Controller"];
    NSString *itemImageName = dict[@"itemImage"];
    NSString *itemImageSelectedName = dict[@"itemImageSelected"];
    NSString *itemTitle = dict[@"itemTitle"];
    NSInteger tag = [dict[@"tag"] integerValue];

    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:Controller];
    
    nvc.tabBarItem.title = itemTitle;
    
    if ( [Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]) {
        NSString *str1 = [NSString stringWithFormat:@"%@%@",Main_Token_Model_MANAGER.postfixIcon,itemImageName];
        
        [nvc.tabBarItem setImage:[kIMAGE_NAME(str1) imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)]];
        
        
        str1 = [NSString stringWithFormat:@"%@%@",Main_Token_Model_MANAGER.postfixIcon,itemImageSelectedName];
        
        [nvc.tabBarItem setSelectedImage:[kIMAGE_NAME(str1) imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)]];
        
        if (@available(iOS 10.0, *)) {
            nvc.tabBarItem.badgeColor = Main_Token_Model_MANAGER.backgroundColor;
        } else {
            // Fallback on earlier versions
        }

    }else{
        [nvc.tabBarItem setImage:[kIMAGE_NAME(itemImageName) imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]];
        [nvc.tabBarItem setSelectedImage: [kIMAGE_NAME(itemImageSelectedName) imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]];
    }
//     Main_Token_Model_MANAGER.backgroundColor



    
    nvc.tabBarItem.tag = tag;
    
    
    

    return nvc;
}

@end
